﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Microsoft.ApplicationBlocks.Data ;
using TransportManager.UI.Pages ;
using System.Data.SqlClient;

namespace Site
{
    public partial class CodeProductType : BasePage
    {
        private string conn;
        protected void Page_Load(object sender, System.EventArgs e)
        {
            if (Session.Count < 2)
            {
               // Response.Write("请登陆");
               // Response.End();
            }
            conn = Application["Conn"].ToString();
            if (!IsPostBack)
            {
                BindData1();
                BindData2();
                BindData3();
                BindData4();
            }
        }
        void BindData1()
        {

            string sql = "select   name + '[' + code + ']' AS name, code from CodeBigType WHERE     (LEN(code) = 1) ";
            var ds = SqlHelper.ExecuteDataset(conn, CommandType.Text, sql);
            drp1.DataTextField = "name";
            drp1.DataValueField = "code";
            drp1.DataSource = ds;
            drp1.DataBind();
        }


        void BindData2()
        {

            string sql = "select   name + '[' + code + ']' AS name, code from CodeBigType WHERE     (LEN(code) = 2) and     (LEFT(code, 1) = '" + drp1.SelectedValue + "') ";
            var ds = SqlHelper.ExecuteDataset(conn, CommandType.Text, sql);
           // Response.Write(sql);
            drp2.DataTextField = "name";
            drp2.DataValueField = "code";
            drp2.DataSource = ds;
            drp2.DataBind();
        }

        void BindData3()
        {

            string sql = "select  name + '[' + code + ']' AS name, code from CodeBigType WHERE     (LEN(code) = 4) and     (LEFT(code, 2) = '" + drp2.SelectedValue + "') ";
            var ds = SqlHelper.ExecuteDataset(conn, CommandType.Text, sql);
           // Response.Write(sql);
            drp3.DataTextField = "name";
            drp3.DataValueField = "code";
            drp3.DataSource = ds;
            drp3.DataBind();
        }

        void BindData4()
        {

            string sql = "select  name + '[' + code + ']' AS name, code from CodeBigType WHERE     (LEN(code) = 6) and     (LEFT(code, 4) = '" + drp3.SelectedValue + "') ";
            var ds = SqlHelper.ExecuteDataset(conn, CommandType.Text, sql);
           // Response.Write(sql);
            drp4.DataTextField = "name";
            drp4.DataValueField = "code";
            drp4.DataSource = ds;
            drp4.DataBind();
        }

 
        protected void drp1_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindData2();
            BindData3();
            BindData4();
        }

        protected void drp2_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindData3();
            BindData4();
        }

        protected void drp3_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindData4();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string code=drp1.SelectedValue;
            string min=drp4.SelectedValue +"100";
            string max=drp4.SelectedValue +"999";
            //string countsql = "select max(number) from dbo.CodeProductType where number>=" + min + " and number<=" + max + "";
            string countsql = "SELECT     MAX(cast(number as int)) num FROM         dbo.CodeProductType WHERE     (cast(number as int) >= "+ min+" ) AND (cast(number as int) <="+ max+") ";
            int count=   Convert.ToInt32(SqlHelper.ExecuteScalar(conn, CommandType.Text, countsql));
            string number=(count+1).ToString();
           
            string sql = "insert into dbo.CodeProductType(code,number,productname,specs,unit,alertamount,ratio,isuse) values('"+code+"',"+number+",'"+txtName.Text+"','"+ txtSpecs.Text+"','"+ txtUnit.Text+"','1','1','1')";
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sql);

            ResponseMsg("添加成功！");
        } 

    }
}